package com.spzx.common.redis.configure;

import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

/**
 * redis配置
 *
 * @author spzx
 */
@Configuration
@EnableCaching //启动spring自动缓存管理！
@AutoConfigureBefore(RedisAutoConfiguration.class)
public class RedisConfig extends CachingConfigurerSupport
{
    //创建spring缓存管理器对象注入到容器中
    //复制粘贴使用！！！！
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory connectionFactory){
        //fastjson2提供的 可以将对象转为json字符串， json字符串还原为对象的 序列化器
        FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
        RedisCacheConfiguration config = RedisCacheConfiguration
                .defaultCacheConfig()//创建redis缓存的默认配置对象
                .entryTtl(Duration.ofSeconds(600)) //redis缓存管理器 设置缓存的过期时间
                //.disableCachingNullValues() //禁止缓存空值： 默认会缓存空值，一般使用默认
                .serializeKeysWith(RedisSerializationContext.SerializationPair
                        .fromSerializer(new StringRedisSerializer())) // 键的序列化器： 设置到缓存中的key如何序列化处理  一般使用字符串序列化方式
                .serializeValuesWith( RedisSerializationContext.SerializationPair
                        .fromSerializer(serializer)); //值的序列化器： redis缓存的值如何序列化

        return RedisCacheManager
                .builder(connectionFactory)//获取RedisCacheManager的构建器，并传入redis的连接工厂对象
                .cacheDefaults(config)  //设置缓存管理的配置
                .build();
    }

    @Bean
    @SuppressWarnings(value = { "unchecked", "rawtypes" })
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
    {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);

        FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);

        // 使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(serializer);

        // Hash的key也采用StringRedisSerializer的序列化方式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(serializer);

        template.afterPropertiesSet();
        return template;
    }
}
